package net.xdclass.controller;


import net.xdclass.controller.request.AccountLoginRequest;
import net.xdclass.controller.request.AccountRegisterRequest;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.service.AccountService;
import net.xdclass.service.FileService;
import net.xdclass.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 小冯
 * @since 2023-06-18
 */
@RestController
@RequestMapping("/api/account/v1")
public class AccountController {

    @Autowired
    private FileService fileService;

    @Autowired
    private AccountService accountService;

    @PostMapping("upload")
    public JsonData uploadUserImg(@RequestPart("file") MultipartFile file) {
        //返回可访问的url
        String result = fileService.upload(file);
        return result != null ? JsonData.buildSuccess(result) : JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);
    }


    @PostMapping("register")
    public JsonData register(@RequestBody AccountRegisterRequest request) {
        //返回可访问的url
        JsonData jsonData = accountService.register(request);
        return jsonData;
    }

    @PostMapping("login")
    public JsonData login(@RequestBody AccountLoginRequest request) {
        //返回可访问的url
        JsonData jsonData = accountService.login(request);
        return jsonData;
    }

}

